// 平滑滚动效果
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
	anchor.addEventListener('click', function(e) {
		e.preventDefault();
		document.querySelector(this.getAttribute('href')).scrollIntoView({
			behavior: 'smooth'
		});
	});
});

// 导航栏激活状态
window.addEventListener('scroll', function() {
	let currentSection = '';
	document.querySelectorAll('section').forEach(section => {
		const sectionTop = section.offsetTop;
		if (pageYOffset >= sectionTop - 100) {
			currentSection = section.getAttribute('id');
		}
	});

	document.querySelectorAll('.nav-link').forEach(link => {
		link.classList.remove('active');
		if (link.getAttribute('href').includes(currentSection)) {
			link.classList.add('active');
		}
	});
});

function setIndexPage() {
	fetch('./pages/header.html')
		.then(response => response.text())
		.then(data => {
			document.body.insertAdjacentHTML('afterbegin', data); // 将头部插入到body的开头
		});
	fetch('./pages/footer.html')
		.then(response => response.text())
		.then(data => {
			document.body.insertAdjacentHTML('beforeend', data); // 将底部插入到body的末尾
		});
}


function setInnerPage() {
	fetch('../pages/header.html')
		.then(response => response.text())
		.then(data => {
			document.body.insertAdjacentHTML('afterbegin', data); // 将头部插入到body的开头
		});
	fetch('../pages/footer.html')
		.then(response => response.text())
		.then(data => {
			document.body.insertAdjacentHTML('beforeend', data); // 将底部插入到body的末尾
		});
}

// 回到顶部
function upToHead() {
	window.scrollTo({
		top: 0,
		behavior: 'smooth'
	});
}